<template>
  <!-- 添加或修改试卷对话框 -->
  <el-dialog :visible.sync="visible" :title="modalObj.title" :modal='modalObj.modal'
             center :close-on-click-modal="true"
             :close-on-press-escape="true"
             @closed="$emit('update:visible', false)"
             width="900px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title" placeholder="请输入标题"/>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="时长" prop="totalTime">
            <el-input v-model="form.totalTime" placeholder="请输入时长"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="及格分" prop="qualifyScore">
            <el-input v-model="form.qualifyScore" placeholder="请输入及格分"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="试卷状态">
        <el-radio-group v-model="form.status">
          <el-radio
            v-for="dict in dict.type.el_paper_status"
            :key="dict.value"
            :label="dict.value"
          >{{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="截止时间" prop="limitTime">
            <el-date-picker clearable
                            v-model="form.limitTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择截止时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="限制次数" prop="limitCount">
            <el-input v-model="form.limitCount" placeholder="请输入限制次数"/>
          </el-form-item>
        </el-col>
      </el-row>


      <el-divider content-position="center">考试题库信息({{ form.totalScore }}分)</el-divider>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddPaperRepository">添加</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeletePaperRepository">删除</el-button>
        </el-col>
      </el-row>
      <el-table :data="paperRepositoryList" :row-class-name="rowPaperRepositoryIndex"
                @selection-change="handlePaperRepositorySelectionChange" ref="paperRepository">
        <el-table-column label="题库" prop="repositoryId" width="200">
          <template slot-scope="scope">
            <el-select v-model="scope.row.repositoryId" placeholder="请选择题库">
              <el-option
                v-for="dict in dict.type.el_repository"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="单选题数量" prop="radioCount" width="100">
          <template slot-scope="scope">
            <el-input v-model="scope.row.radioCount" placeholder="数量" type="number"/>
          </template>
        </el-table-column>
        <el-table-column label="单选题分数" prop="radioScore" width="100">
          <template slot-scope="scope">
            <el-input v-model="scope.row.radioScore" placeholder="分数" type="number"/>
          </template>
        </el-table-column>
        <el-table-column label="多选题数量" prop="multiCount" width="100">
          <template slot-scope="scope">
            <el-input v-model="scope.row.multiCount" placeholder="数量" type="number"/>
          </template>
        </el-table-column>
        <el-table-column label="多选题分数" prop="multiScore" width="100">
          <template slot-scope="scope">
            <el-input v-model="scope.row.multiScore" placeholder="分数" type="number"/>
          </template>
        </el-table-column>
        <el-table-column label="填空题数量" prop="fillCount" width="100">
          <template slot-scope="scope">
            <el-input v-model="scope.row.fillCount" placeholder="数量" type="number"/>
          </template>
        </el-table-column>
        <el-table-column label="填空题分数" prop="fillScore" width="100">
          <template slot-scope="scope">
            <el-input v-model="scope.row.fillScore" placeholder="分数" type="number"/>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getPaper, addPaper, updatePaper} from '@/api/exam/paper'

export default {
  name: 'PaperEdit',
  dicts: ['el_repository', 'el_paper_status'],

  props: {
    visible: {
      type: Boolean,
      default: false
    },
    modalObj: {
      type: Object,
      default: false
    }
  },
  watch: {
    paperRepositoryList: {
      handler(v) {

        let sum = 0;
        v.forEach(o => {
          sum += o.radioCount * o.radioScore + o.multiCount * o.multiScore
        })
        this.form.totalScore = sum;
      }, deep: true
    }
  },
  data() {
    return {
      // 考试题库表格数据
      paperRepositoryList: [],
      // 表单参数
      form: {totalScore: 0},
      // 表单校验
      rules: {
        title: [
          {required: true, message: '标题不能为空', trigger: 'blur'}
        ],
        totalTime: [
          {required: true, message: '时长不能为空', trigger: 'blur'}
        ],
        totalScore: [
          {required: true, message: '总分不能为空', trigger: 'blur'}
        ],
        qualifyScore: [
          {required: true, message: '及格分不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.reset()
    this.getData()
  },
  methods: {
    /** 查询试卷列表 */
    getData() {
      if (this.modalObj.id) {
        getPaper(this.modalObj.id).then(response => {
          this.form = response.data
          this.paperRepositoryList = response.data.paperRepositoryList
        })
      }
    },
    // 取消按钮
    cancel() {
      this.$emit('handleClose', {component: 'openEdit'})
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        totalTime: null,
        totalScore: null,
        qualifyScore: null,
        status: '0',
        createTime: null,
        updateTime: null,
        limitTime: null,
        limitCount: null
      }
      this.paperRepositoryList = []
      this.resetForm('form')
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.paperRepositoryList = this.paperRepositoryList
          if (this.form.id != null) {
            updatePaper(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.$emit('handleClose', {component: 'openEdit', get: true})
            })
          } else {
            addPaper(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.$emit('handleClose', {component: 'openEdit', get: true})
            })
          }
        }
      })
    },
    /** 考试题库序号 */
    rowPaperRepositoryIndex({row, rowIndex}) {
      row.index = rowIndex + 1
    },
    /** 考试题库添加按钮操作 */
    handleAddPaperRepository() {
      let obj = {}
      obj.repositoryId = ''
      obj.radioCount = ''
      obj.radioScore = ''
      obj.multiCount = ''
      obj.multiScore = ''
      obj.fillCount = ''
      obj.fillScore = ''
      this.paperRepositoryList.push(obj)
    },
    /** 考试题库删除按钮操作 */
    handleDeletePaperRepository() {
      if (this.checkedPaperRepository.length == 0) {
        this.$modal.msgError('请先选择要删除的考试题库数据')
      } else {
        const paperRepositoryList = this.paperRepositoryList
        const checkedPaperRepository = this.checkedPaperRepository
        this.paperRepositoryList = paperRepositoryList.filter(function (item) {
          return checkedPaperRepository.indexOf(item.index) == -1
        })
      }
    },
    /** 复选框选中数据 */
    handlePaperRepositorySelectionChange(selection) {
      this.checkedPaperRepository = selection.map(item => item.index)
    }

  }
}
</script>
